﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Windows.Forms.DataVisualization.Charting;

namespace SecuritiesAnalysis.ToolkitWinForm
{
    public partial class frmHistoryView : Form
    {

        private SecuritiesAnalysisDataContext ctx;
        private List<DateTime> datelist;
        private List<T_HistoryPrice> historyList;
        private List<int> volList;

        private List<decimal> openList;
        private List<decimal> closeList;
        private List<decimal> ceilList;
        private List<decimal> floorList;


        public frmHistoryView()
        {
            InitializeComponent();

            // First set the ChartArea.InnerPlotPosition property.
            chart1.ChartAreas["Default"].InnerPlotPosition.Auto = true;
            chart1.ChartAreas["Volume"].InnerPlotPosition.Auto = true;

            // Set the alignment properties so the "Volume" chart area will allign to "Default"
            chart1.ChartAreas["Volume"].AlignmentOrientation = AreaAlignmentOrientations.Vertical;
            chart1.ChartAreas["Volume"].AlignmentStyle = AreaAlignmentStyles.All;
            chart1.ChartAreas["Volume"].AlignWithChartArea = "Default";

            ctx = new SecuritiesAnalysisDataContext();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            databindXY();
        }

        private void databindXY()
        {
            historyList=ctx.T_HistoryPrices.Where(
                o=>o.StockCode==(int)(this.numericStockCode.Value)
                    )
                                    .OrderByDescending(c => c.Date)
                .Take((int)this.numericDays.Value)
                .ToList();

            this.datelist = new List<DateTime>();
            this.volList = new List<int>();
            this.openList = new List<decimal>();
            this.closeList = new List<decimal>();
            this.ceilList = new List<decimal>();
            this.floorList = new List<decimal>();

            foreach (T_HistoryPrice mod in historyList)
            {
                datelist.Add(mod.Date);
                volList.Add(mod.Volume.Value/10000);

                this.openList.Add(mod.OpeningPrice.Value);
                this.closeList.Add(mod.ClosingPrice.Value);
                this.ceilList.Add(mod.CeilingPrice.Value);
                this.floorList.Add(mod.FloorPrice.Value);

                //chart1.Series["Input"].Points.AddXY(
                //    mod.Date,
                //                    mod.OpeningPrice.Value,
                //mod.ClosingPrice.Value,
                //    mod.CeilingPrice.Value,
                //    mod.FloorPrice.Value

                //);
            }

            chart1.Series["Volume"].Points.DataBindXY(datelist, volList);
            //顺序极其重要
            chart1.Series["Input"].Points.DataBindXY(datelist, this.floorList, this.ceilList, this.openList, this.closeList);

        }

        private void chart1_CursorPositionChanging(object sender, CursorEventArgs e)
        {
            SetPosition(e.Axis, e.NewPosition);
        }

        private void chart1_CursorPositionChanged(object sender, CursorEventArgs e)
        {
            SetPosition(e.Axis, e.NewPosition);
        }

        // Set Cursor Position to display control.
        private void SetPosition(Axis axis, double position)
        {
            if (double.IsNaN(position))
                return;

            StringBuilder cursorinfo = new StringBuilder();

            if (axis.AxisName == AxisName.X)
            {
                // Convert Double to DateTime.
                DateTime dateTimeX = DateTime.FromOADate(position);
                cursorinfo.Append("日期：");
                cursorinfo.Append(dateTimeX.ToShortDateString());

                T_HistoryPrice currenthistory = this.historyList.Where(o => o.Date == dateTimeX).FirstOrDefault();
                if (currenthistory == null)
                {
                    cursorinfo.Append("  休市");
                    chart1.Titles[0].Text = cursorinfo.ToString();
                }
                else
                {
                    cursorinfo.Append("  开盘：");
                    cursorinfo.Append(currenthistory.OpeningPrice.Value.ToString());
                    cursorinfo.Append("  收盘：");
                    cursorinfo.Append(currenthistory.ClosingPrice.Value.ToString());
                    cursorinfo.Append("  最高：");
                    cursorinfo.Append(currenthistory.CeilingPrice.Value.ToString());
                    cursorinfo.Append("  最低：");
                    cursorinfo.Append(currenthistory.FloorPrice.Value.ToString());
                    cursorinfo.Append("  成交量：");
                    cursorinfo.Append(currenthistory.Volume.Value.ToString());

                    chart1.Titles[0].Text = cursorinfo.ToString();
                }
            }
        }
    }
}
